import {Component, EventEmitter, Input, Output} from "@angular/core";
import {ActivityStatus} from "../../../../model/pages/activity/activity-filter/activity-filter.model";
import {NavController} from "ionic-angular";
import {ClientNoteLogModel} from "../../../../model/pages/client/client-note/client-note-log/client-note-log.model";
import {CallNumberService} from "../../../../services/native/call-number/call-number.service";

@Component({
    selector: 'roster-card',
    templateUrl: 'roster-card.component.html',
    providers: [CallNumberService]
})
export class RosterCardComponent {
    @Input() roster;
    @Input() index;
    @Input() classify;
    @Input() isSingle;

    @Output() switchUserStatus = new EventEmitter();
    @Output() showAction = new EventEmitter();
    @Output() switchSegment = new EventEmitter();

    //当前能否打电话
    private canCall = true;

    constructor(private navCtrl: NavController,
                private callNumberService: CallNumberService) {
    }

    /**
     * 是否是已经结束活动
     */
    isOverTime(status) {
        return status === ActivityStatus.over || status === ActivityStatus.notStart;
    }

    /**
     * 拨打电话
     */
    callPhone(detail, ev) {
        if (detail.tel && this.canCall) {
            this.canCall = false;
            let clientLogModel = new ClientNoteLogModel();
            clientLogModel.clientType = this.classify === 0 ? detail.clientType : 3;
            clientLogModel.noteType = 4;
            clientLogModel.content = '拨打电话';
            clientLogModel.contentType = 1;
            clientLogModel.clientNumber = detail.id + '';
            if (this.classify === 0) {
                this.callNumberService.makeCall(detail.tel + '',
                    clientLogModel, true, {
                        name: detail.name,
                        customerId: detail.id,
                        tel: detail.tel
                    }, () => {
                        // this.switchUserStatus(detail.id);
                        this.switchUserStatus.emit(detail.id);
                    }, () => {
                    }, () => {
                        this.canCall = true;
                    });
            } else {
                this.callNumberService.makeCall(detail.tel, clientLogModel, false, {
                        name: detail.name,
                        customerId: detail.id,
                        tel: detail.tel
                    }, () => {
                    }, () => {
                    },
                    () => {
                        this.canCall = true;
                    });
            }
        }
        ev.stopPropagation();
    }

    /**
     * 展示
     */
    showSelectAction(innerId, innerIndex, $event) {
        let o = {
            innerId, innerIndex, $event
        };
        this.showAction.emit(o);
    }

    /**
     * 跳转到名单详情
     */
    goToRosterDetail(clientId) {
        this.navCtrl.push('RosterDetailPage', {
            clientId,
            classify: this.classify
        });
    }

    /**
     * 跳转到活动详情
     */
    toActivityDetail(id) {
        if (this.isSingle) {
            this.switchSegment.emit('toDetail');
        } else {
            this.navCtrl.push('ActivitDetailPage', {
                id,
                classify: this.classify
            });
        }
    }
}
